<?php
namespace App\admin\Controller;
use think\Controller;
use think\Db;
use think\Request;

class Api extends Controller{

  public function swiperlists(){
    $res=db('swiper')->where('activity_id!=0')->field("swiper_id,swiper_bpic,activity_id")->select();
    for($i=0; $i < sizeof($res);$i++)
   {
     $res[$i]['swiper_bpic'] = "http://localhost/gym/public/static/images/".$res[$i]['swiper_bpic'];
   }
    $message = ["message"=>$res,"msg"=>'获取成功',"status"=>200];
    return json($message);
  }

  public function cateslists(){
    $res=db('cart')->select();
    for($i=0; $i < sizeof($res);$i++)
   {
     $res[$i]['cart_pic'] = "http://localhost/gym/public/static/images/".$res[$i]['cart_pic'];
   }
    $message = ["message"=>$res,"msg"=>'获取成功',"status"=>200];
    return json($message);
  }

  public function floorlists(){
    $res=db('floor')->where('floor_level',0)->select();
    for($i=0; $i < sizeof($res);$i++)
   {
     $res[$i]['floor_pic'] = "http://localhost/gym/public/static/images/".$res[$i]['floor_pic'];
     $resi = db('floor')->where('floor_pid',$res[$i]['floor_id'])->where('floor_level',1)->select(); 
     for($j=0; $j < sizeof($resi);$j++){
    $resi[$j]['floor_pic'] = "http://localhost/gym/public/static/images/".$resi[$j]['floor_pic'];
     }
     $res[$i]['product_list']=$resi;
   }
    $message = ["message"=>$res,"msg"=>'获取成功',"status"=>200];
    return json($message);
  }

  function categories(){
    $res=db('classify')->where('classify_level',0)->select();
    for($i=0;$i<count($res[0]);$i++){
      $resi = db('classify')->where('classify_pid',$res[$i]['classify_id'])->where('classify_level',1)->select(); 
      
      if($resi)
      {                    
        for($j =0 ;$j<count($resi[0]);$j++){            
          if($resi[$j]['classify_id']==$res[$i]['classify_id'])  
          continue;
          $resj = db('classify')->where('classify_level',2)->where('classify_pid',$resi[$j]['classify_id'])->select();       
          if($resj)
          {                
            for($k =0 ;$k<count($resj[0]);$k++)               
            if($resj[$k]['classify_icon']!=null)
              $resj[$k]['classify_icon'] = "http://localhost/gym/public/static/images/".$resj[$k]['classify_icon'];
             $resi[$j]['children']=$resj;  
          }        
        }
        if($resi!=null)
        $res[$i]['children']=$resi;
      }
    }
    $message = ["message"=>$res,"msg"=>'获取成功',"status"=>200];
    return json($message);
  }

  public function goodsdetail(){
    $goods_id=Request::instance()->param('goods_id');
    $res=db('goods')->where('goods_id',$goods_id)->select();
    for($i=0; $i < sizeof($res);$i++)
   {
     $res[$i]['goods_pic'] = "http://localhost/gym/public/static/images/".$res[$i]['goods_pic'];
  
       }
    $message = ["message"=>$res[0],"msg"=>'获取成功',"status"=>200];
    return json($message);
  }

  public function goodsearch(){
    $cid=Request::instance()->param('cid');
    $res=db('goods')->where('classify_id',$cid)->field("goods_id,goods_name ,goods_price ,goods_pic")->select();    
    for($i=0; $i < sizeof($res);$i++)
   {
     $res[$i]['goods_pic'] = "http://localhost/gym/public/static/images/".$res[$i]['goods_pic'];    
   }      
    $message = ["total"=>sizeof($res),'goods'=>$res,"msg"=>'获取成功',"status"=>200];
    return json($message);
  }

  public function qsearch(){
    $query = input('query');
    $res = db('goods')->where('goods_name', 'LIKE', "%$query%")->select();
    $message = ["message"=>$res,"msg"=>'获取成功',"status"=>200];
    return json($message);
}

public function log(){
  $res=db('log')->alias('l')->join('user u','u.user_id=l.user_id')->field("log_id,user_name,user_pic,log_desc,log_update,l.user_id")->select();
    for($i=0; $i<sizeof($res);$i++){
      if(substr($res[$i]['user_pic'], 0, strlen("https://wx.qlogo.cn/")) === "https://wx.qlogo.cn/") {
        continue;
      }
     $res[$i]['user_pic'] = "http://localhost/gym/public/static/images/".$res[$i]['user_pic'];
   }

    for($j=0;$j<count($res);$j++){
    $res1=db('lpic')->where('log_id',$res[$j]['log_id'])->field("lpic_pic")->select();
      if($res1){
        for($k=0; $k<sizeof($res1);$k++){
         $res1[$k]['lpic_pic'] = "http://localhost/gym/public/static/images/".$res1[$k]['lpic_pic'];
    }
         $res[$j]['logpic'] = $res1; 
  } 
 }
    
    for($l=0; $l<count($res[0]);$l++)
    {      
      $res2=db('like')->where('log_id',$res[$l]['log_id'])->field("user_id")->select();
      if($res2[0]['user_id']!=null)
      {        
        $res[$l]['like']=[];
       for($m=0;$m<count($res2);$m++)
       {
        $res3=db('user')->where('user_id',$res2[$m]['user_id'])->field('user_id,user_name')->select();  
        if($res3[0]['user_name']!=null)
        array_push($res[$l]['like'],$res3[0]);                
       }   
      }         
      else
      continue;       
   } 
  
    for($y=0;$y<count($res);$y++)
    {
     $res4=db('comment')->where('log_id',$res[$y]['log_id'])->field("user_id,comment_desc")->select();  
     
      if($res4!=null)
      {
        $res[$y]['comment']=[];
        $res4[$y]['user_name']=[];
        for($p=0;$p<count($res4);$p++)
        {
        $res5=db('user')->where('user_id',$res4[$p]['user_id'])->field('user_name')->select(); 
        if($res5[0]['user_name']!=null)
        {
          $res4[$p]['user_name'] = $res5[0]['user_name'];
          array_push($res[$y]['comment'],$res4[$p]);
        }
        }
      }
        else
        continue;
      }
    $message = ["message"=>$res,"msg"=>'获取成功',"status"=>200];
    
    return json($message);
}

public function setUserInfo(){
  
    $userInfo = json_decode(input("userInfo"));
    $code = input('code');

    $url = "https://api.weixin.qq.com/sns/jscode2session?appid=wxda901780c18bdf44&secret=608cbaa689247512bb9d75580fc20733&js_code=" . $code . "&grant_type=authorization_code";
    $data = curl_get_https($url);
    $data = json_decode($data, true);

    if ($data != null) {
      $openId = $data['openid'];
      $user = db('user')->where('Openid', $openId)->find();
      if (!$user) {
        $res = db('user')->insert([
          'user_name'=> $userInfo->nickName,
          'user_gender' => $userInfo->gender,
          'user_age' => 0,
          'user_pic' => $userInfo->avatarUrl,
          'user_phone' => "null",
          'user_date' => date('Y-m-d H:i:s', time()),
          'Openid' => $openId
        ]);
        if($res) {
          $user = db('user')->where('Openid', $openId)->find();
          $message = ["message"=>$user ,"msg"=>'setUserInfo: ok',"status"=>200];
          return json($message);
        } else {
          // TODO 插入失败
        } 
      }else {
        $message = ["message"=>$user ,"msg"=>'setUserInfo: ok',"status"=>200];
        return json($message);
      }
    } else {
    // TODO 服务器与微信服务器连接出错
    }
}
public function uplog_content(){
    $userId = input('userId');
    $content = input('content');
    $flag = input('flag');
    $res = db('log')->insert([
      'log_desc' => $content,
      'log_status' => $flag,
      'log_update' => date('Y-m-d H:i:s', time()),
      'user_id' => $userId
    ]);
    if ($res) {

      return json([
        'logId' => db('log')->getLastInsID()
      ]);
    } else {
      // TODO 插入失败
    }
}

public function uplog_files() {
  $userId = input('user_id');
  $logId = input('log_id');
  $flag = input('flag');
  $file = $_FILES['photo'];
  $dir = ROOT_PATH . DS . 'public/static/images/logs/';

  $log = db('log')->where('log_id', $logId)->find();
  if ($log && $log['log_status'] == "1") {
    $message = ["message"=>"","msg"=>'数据库错误',"status"=>0];
    return json($message);
  }

  $filename = md5(microtime(true)).'.'.explode('.',$file['name'])[count(explode('.',$file['name']))-1];
  $filepath = $dir.$filename;

  if(!file_exists($dir)){
      mkdir($dir,0777,true);
  }

  if(move_uploaded_file($file['tmp_name'], $filepath)){
    $res = db('lpic')->insert([
      'lpic_pic' => $filename,
      'log_id' => $logId,
      'user_id' => $userId
    ]);
    if ($res) {
      if ($flag == "1") {
        db('log')->where('log_id', $logId)->update([
          'log_status' => "1"
        ]);
      }
      $message = ["message"=>"","msg"=>'上传图片成功',"status"=>200];
      return json($message);
    }
  }
}

public function zan() {
  $userId = input('user_id');
  $logId = input('log_id');
  $res = db('like')->where('user_id', $userId)->where('log_id', $logId)->find();
  $status = null;
  if ($res) {
    $res = db('like')->where('user_id', $userId)->where('log_id', $logId)->delete();
    if ($res) {
      $status = 0;
    }
  }else {
    $res = db('like')->insert([
      'user_id' => $userId,
      'log_id' => $logId
    ]);
    if ($res) {
      $status = 1;
    }
  }
  $message = ["message"=>$status,"msg"=>'处理赞成功',"status"=>200];
  return json($message);
}

public function comment() {
  $userId = input('user_id');
  $logId = input('log_id');
  $comment = input('comment');
  $res = db('comment')->insert([
    'user_id' => $userId,
    'log_id' => $logId,
    'comment_desc' => $comment,
    'comment_time' => date('Y-m-d H:i:s', time())
  ]);

  if ($res) {
    $message = ["message"=>"","msg"=>'评论成功',"status"=>200];
    return json($message);
  }
}

public function delete1() {
  $userId = input('user_id');
  $logId = input('log_id');
    $res = db('like')->where('log_id', $logId)->delete();
    $res1= db('lpic')->where('log_id', $logId)->delete();
    $res2= db('comment')->where('log_id', $logId)->delete();
    $res3=db('log')->where('log_id', $logId)->delete();
  $message = ["message"=>"","msg"=>'删除成功',"status"=>200];
  return json($message);
}
}


